// type defined through parameter
if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
- $name = $this->getCliArg( 'use-bagostuff' );
+ global $wgObjectCaches;
- $this->cache = ObjectCache::newFromId( $name );
+ $id = $this->getCliArg( 'use-bagostuff' );
+ $this->cache = ObjectCache::newFromParams( $wgObjectCaches[$id] );
} else {
// no type defined - use simple hash
$this->cache = new HashBagOStuff;
* @covers MediumSpecificBagOStuff::makeKeyInternal
*/
public function testMakeKey() {
- $cache = ObjectCache::newFromId( 'hash' );
+ $cache = new HashBagOStuff();
$localKey = $cache->makeKey( 'first', 'second', 'third' );
$globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
/**
* @covers MediumSpecificBagOStuff::changeTTL
*/
- public function testChangeTTL() {
- $now = 1563892142;
+ public function testChangeTTLRenew() {
+ $now = microtime( true ); // need real time
$this->cache->setMockTime( $now );
$key = $this->cache->makeKey( self::TEST_KEY );
$value = 'meow';
- $this->cache->add( $key, $value, 5 );
+ $this->cache->add( $key, $value, 60 );
$this->assertEquals( $value, $this->cache->get( $key ) );
- $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
- $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
+ $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
+ $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
$this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
$this->assertEquals( $this->cache->get( $key ), $value );
+
$this->cache->delete( $key );
$this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
+ }
+
+ /**
+ * @covers MediumSpecificBagOStuff::changeTTL
+ */
+ public function testChangeTTLExpireRel() {
+ $now = microtime( true ); // need real time
+ $this->cache->setMockTime( $now );
+
+ $key = $this->cache->makeKey( self::TEST_KEY );
+ $value = 'meow';
+
+ $this->cache->add( $key, $value, 5 );
+ $this->assertTrue( $this->cache->changeTTL( $key, -3600 ) );
+ $this->assertFalse( $this->cache->get( $key ) );
+ }
+
+ /**
+ * @covers MediumSpecificBagOStuff::changeTTL
+ */
+ public function testChangeTTLExpireAbs() {
+ $now = microtime( true ); // need real time
+ $this->cache->setMockTime( $now );
+
+ $key = $this->cache->makeKey( self::TEST_KEY );
+ $value = 'meow';
$this->cache->add( $key, $value, 5 );
$this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );
$key4 = $this->cache->makeKey( 'test-key4' );
// cleanup
- $this->cache->delete( $key1 );
- $this->cache->delete( $key2 );
- $this->cache->delete( $key3 );
- $this->cache->delete( $key4 );
+ $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
$ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
$this->assertFalse( $ok, "No keys found" );
$this->assertFalse( $this->cache->get( $key3 ) );
$ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
-
$this->assertTrue( $ok, "setMulti() succeeded" );
$this->assertEquals(
3,
$this->assertEquals( 2, $this->cache->get( $key2 ) );
$this->assertEquals( 3, $this->cache->get( $key3 ) );
- $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
- $this->assertTrue( $ok, "Expiry set for all keys" );
-
$ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
$this->assertFalse( $ok, "One key missing" );
+ $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
+
+ $now = microtime( true ); // real time
+ $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
+ $this->assertTrue( $ok, "setMulti() succeeded" );
+
+ $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
+ $this->assertTrue( $ok, "Expiry set for all keys" );
+ $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
$this->assertEquals( 2, $this->cache->incr( $key1 ) );
$this->assertEquals( 3, $this->cache->incr( $key2 ) );
$this->assertEquals( 4, $this->cache->incr( $key3 ) );
// cleanup
- $this->cache->delete( $key1 );
- $this->cache->delete( $key2 );
- $this->cache->delete( $key3 );
- $this->cache->delete( $key4 );
+ $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
}
/**
*/
public function testGetWithSetCallback() {
$now = 1563892142;
- $this->cache->setMockTime( $now );
- $key = $this->cache->makeKey( self::TEST_KEY );
+ $cache = new HashBagOStuff( [] );
+ $cache->setMockTime( $now );
+ $key = $cache->makeKey( self::TEST_KEY );
- $this->assertFalse( $this->cache->get( $key ), "No value" );
+ $this->assertFalse( $cache->get( $key ), "No value" );
- $value = $this->cache->getWithSetCallback(
+ $value = $cache->getWithSetCallback(
$key,
30,
function ( &$ttl ) {
);
$this->assertEquals( 'hello kitty', $value );
- $this->assertEquals( $value, $this->cache->get( $key ), "Value set" );
+ $this->assertEquals( $value, $cache->get( $key ), "Value set" );
$now += 11;
- $this->assertFalse( $this->cache->get( $key ), "Value expired" );
+ $this->assertFalse( $cache->get( $key ), "Value expired" );
}
/**
$val = $this->cache->incrWithInit( $key, 0, 1, 3 );
$this->assertEquals( 4, $val, "Correct init value" );
+ $this->cache->delete( $key );
+
+ $val = $this->cache->incrWithInit( $key, 0, 5 );
+ $this->assertEquals( 5, $val, "Correct init value" );
}
/**
return $oldValue . '!';
};
- foreach ( [ $tiny, $small, $big ] as $value ) {
+ $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
+ foreach ( $cases as $case => $value ) {
$this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
- $this->assertEquals( $value, $this->cache->get( $key ) );
- $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key] );
-
- $this->assertTrue( $this->cache->merge( $key, $callback, 5 ) );
- $this->assertEquals( "$value!", $this->cache->get( $key ) );
- $this->assertEquals( "$value!", $this->cache->getMulti( [ $key ] )[$key] );
-
- $this->assertTrue( $this->cache->deleteMulti( [ $key ] ) );
- $this->assertFalse( $this->cache->get( $key ) );
- $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+ $this->assertEquals( $value, $this->cache->get( $key ), "get $case" );
+ $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key], "get $case" );
+
+ $this->assertTrue(
+ $this->cache->merge( $key, $callback, 5, 1, BagOStuff::WRITE_ALLOW_SEGMENTS ),
+ "merge $case"
+ );
+ $this->assertEquals(
+ "$value!",
+ $this->cache->get( $key ),
+ "merged $case"
+ );
+ $this->assertEquals(
+ "$value!",
+ $this->cache->getMulti( [ $key ] )[$key],
+ "merged $case"
+ );
+
+ $this->assertTrue( $this->cache->deleteMulti( [ $key ] ), "delete $case" );
+ $this->assertFalse( $this->cache->get( $key ), "deleted $case" );
+ $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "deletd $case" );
$this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
- $this->assertEquals( "@$value", $this->cache->get( $key ) );
- $this->assertTrue( $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ) );
- $this->assertFalse( $this->cache->get( $key ) );
- $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+ $this->assertEquals( "@$value", $this->cache->get( $key ), "get $case" );
+ $this->assertTrue(
+ $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ),
+ "prune $case"
+ );
+ $this->assertFalse( $this->cache->get( $key ), "pruned $case" );
+ $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "pruned $case" );
}
$this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );